class NaturezasController < ApplicationController
  # GET /naturezas
  # GET /naturezas.xml
  def index
    @naturezas = Natureza.all()

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @naturezas }
      format.json { render :json => @naturezas }
    end
  end

  # GET /naturezas/1
  # GET /naturezas/1.xml
  def show
    @natureza = Natureza.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @natureza }
    end
  end

  # GET /naturezas/new
  # GET /naturezas/new.xml
  def new
    @natureza = Natureza.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @natureza }
    end
  end

  # GET /naturezas/1/edit
  def edit
    @natureza = Natureza.find(params[:id])
  end

  # POST /naturezas
  # POST /naturezas.xml
  def create
    
    # salvamos e deletamos o parent_id do hash
    # de parâmetros a fim de evitar uma warning de
    # "Can't mass-assign these protected attributes",
    # pois o atributo 'parent_id' é protected no model.
#    pai_id = params[:natureza][:parent_id]
#    params[:natureza].delete("parent_id")
    
    @natureza = Natureza.new(params[:natureza])

    respond_to do |format|
      if @natureza.save
#        unless pai_id == nil || pai_id == ''
#          pai = Natureza.find(pai_id)
#          @natureza.move_to_child_of pai
#        end
        flash[:notice] = 'Natureza was successfully created.'
        format.html { redirect_to(@natureza) }
        format.xml  { render :xml => @natureza, :status => :created, :location => @natureza }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @natureza.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /naturezas/1
  # PUT /naturezas/1.xml
  def update
    @natureza = Natureza.find(params[:id])

    respond_to do |format|
      if @natureza.update_attributes(params[:natureza])
        flash[:notice] = 'Natureza was successfully updated.'
        format.html { redirect_to(@natureza) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @natureza.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /naturezas/1
  # DELETE /naturezas/1.xml
  def destroy
    @natureza = Natureza.find(params[:id])
    @natureza.destroy

    respond_to do |format|
      format.html { redirect_to(naturezas_url) }
      format.xml  { head :ok }
    end
  end
end
